using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

namespace EquityXOI
{
    public class AuthenticationConfigHandler : IConfigurationSectionHandler
    {
        #region IConfigurationSectionHandler Members

        public object Create(object parent, object configContext, XmlNode section)
        {
            IList<AuthenticationConfigModel> lstConfig = new List<AuthenticationConfigModel>();

            foreach (XmlNode node in section.SelectNodes("AuthenticationSettions"))
            {
                if (node != null)
                {
                    AuthenticationConfigModel configModel = new AuthenticationConfigModel();

                    configModel.Name = node.Attributes["name"].Value;

                    bool bEnable = false;
                    bool.TryParse(node.Attributes["enable"].Value, out bEnable);
                    configModel.Enable = bEnable;

                    configModel.Url = node.SelectSingleNode("AuthenticationUrl/Urls/BaseUrl").InnerText;

                    Dictionary<string, string> nameValueCollection = new Dictionary<string, string>();

                    foreach (XmlNode cnode in node.SelectNodes("AuthenticationUrl/QueryString/Key"))
                    {
                        if (cnode != null)
                        {
                            nameValueCollection.Add(cnode.Attributes["name"].Value, cnode.Attributes["value"].Value);
                        }
                    }
                    configModel.NameValueCollection = nameValueCollection;

                    configModel.UsefulLife = GetUsefulLife(node.SelectSingleNode("UsefulLife"));

                    ParseTestLoginSection(node.SelectSingleNode("TestLogin"), configModel);

                    lstConfig.Add(configModel);
                }
            }

            return lstConfig;
        }

        #endregion

        private long GetUsefulLife(XmlNode node)
        {
            if (node == null) return -1;

            long res = 0;
            string val = node.InnerText;

            Regex reg = new Regex(@"(^(((?<day>\d+)(d|D))?((?<hour>\d+)(h|H))?((?<minute>\d+)(m|M))?((?<second>\d+)(s|S))?)$)|(^(?<hour>\d+)+(:{1}(?<minute>\d+))?(:{1}(?<second>\d+))?$)");

            if (reg.IsMatch(val))
            {
                int Day = 0;
                int Hour = 0;
                int Minute = 0;
                int Second = 0;
                int.TryParse(reg.Match(val).Groups["day"].ToString(), out Day);
                int.TryParse(reg.Match(val).Groups["Hour"].ToString(), out Hour);
                int.TryParse(reg.Match(val).Groups["Minute"].ToString(), out Minute);
                int.TryParse(reg.Match(val).Groups["Second"].ToString(), out Second);

                res = Day * 24 * 3600 + Hour * 3600 + Minute * 60 + Second;
            }

            return res;
        }

        private void ParseTestLoginSection(XmlNode testLoginSection,AuthenticationConfigModel configModel)
        {
            if (testLoginSection == null) return;

            configModel.TestUrl = testLoginSection.SelectSingleNode("TestUrl").Value;
            configModel.SuccessFeature = testLoginSection.SelectSingleNode("SuccessFeature").Value;
            configModel.FailedFeature = testLoginSection.SelectSingleNode("FailedFearute").Value;
            return;
        }
    }
}